/**==============================================================================
            Copyright (c) 2013 Moritz Hader, Marcel Schrder
            All Rights Reserved.

	@file  HeimdallEGLContextFactory.java

	@brief  This class is used by the GLSurfaceView to create a new EGLContext. 
	It is up to the context to provide the buffers and functionalities that are 
	necessary to render vertex-data to the display (except of the functionalities 
	that are provided by the Khronos-API itself of course).

  This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
==============================================================================*/
package de.ibm.dhbw.heimdall.egl;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;

import android.opengl.GLSurfaceView;

/**
 * 
 * @author moritzhader
 *
 */
public class HeimdallEGLContextFactory implements GLSurfaceView.EGLContextFactory{
	private static Logger Log = Logger.getLogger(HeimdallEGLContextFactory.class.getCanonicalName());
	
	private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
	
	@Override
	public EGLContext createContext(EGL10 egl, EGLDisplay display,
			EGLConfig eglConfig) {

		Log.log(Level.INFO, "Creating the EGLContext using GLSurfaceView.EGLContextFactory");
		/*
		 * At this point, we need to tell the surfaceView which type of EGLContext is to be used, as
		 * the context as well as the later controll of the rendering differes with the EGL-Version.
		 * To do this, we make an EGL Attribute-List that contains out configuration-Key.
		 */
		int[] attributes = {
				EGL_CONTEXT_CLIENT_VERSION, 2, //By choosing EGL2.0, we are able to make use of the new features instead of the default Android-implementation
				egl.EGL_NONE
		};
		
		/*
		 * Given the attributes, we can now ask the ConfigChooser to find an apt context for our needs
		 */
		
		
		return egl.eglCreateContext(display, eglConfig, egl.EGL_NO_CONTEXT, attributes);
	}

	@Override
	public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
		Log.log(Level.INFO, "Destroying the EGLContext created by GLSurfaceView.EGLContextFactory");
		egl.eglDestroyContext(display, context);
	}

}
